home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Main.bin / MenuComponent.java < prev    next >
Text File  |  1998-09-22  |  8KB  |  248 lines

  1. /*
  2.  * @(#)MenuComponent.java    1.29 98/08/21
  3.  *
  4.  * Copyright 1995-1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  * 
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14. package java.awt;
  15.  
  16. import java.awt.peer.MenuComponentPeer;
  17. import java.awt.event.ActionEvent;
  18.  
  19. /**
  20.  * The abstract class <code>MenuComponent</code> is the superclass 
  21.  * of all menu-related components. In this respect, the class
  22.  * <code>MenuComponent</code> is analogous to the abstract superclass
  23.  * <code>Component</code> for AWT components.
  24.  * <p>
  25.  * Menu components receive and process AWT events, just as components do,
  26.  * through the method <code>processEvent</code>.
  27.  *
  28.  * @version     1.29, 08/21/98
  29.  * @author     Arthur van Hoff
  30.  * @since       JDK1.0
  31.  */
  32. public abstract class MenuComponent implements java.io.Serializable {
  33.     transient MenuComponentPeer peer;
  34.     transient MenuContainer parent;
  35.     Font font;
  36.     private String name;
  37.     private boolean nameExplicitlySet = false;
  38.  
  39.     boolean newEventsOnly = false;
  40.  
  41.     /*
  42.      * Internal constants for serialization 
  43.      */
  44.     final static String actionListenerK = Component.actionListenerK;
  45.     final static String itemListenerK = Component.itemListenerK;
  46.  
  47.     /*
  48.      * JDK 1.1 serialVersionUID 
  49.      */
  50.     private static final long serialVersionUID = -4536902356223894379L;
  51.  
  52.     /**
  53.      * Construct a name for this MenuComponent.  Called by getName() when
  54.      * the name is null.
  55.      */
  56.     String constructComponentName() {
  57.     return null; // For strict compliance with prior JDKs, a MenuComponent
  58.                  // that doesn't set its name should return null from
  59.                  // getName()
  60.     }
  61.  
  62.     /**
  63.      * Gets the name of the menu component.
  64.      * @return        the name of the menu component.
  65.      * @see           java.awt.MenuComponent#setName(java.lang.String)
  66.      * @since         JDK1.1
  67.      */
  68.     public String getName() {
  69.     if (name == null && !nameExplicitlySet) {
  70.         synchronized(this) {
  71.         if (name == null && !nameExplicitlySet) {
  72.             name = constructComponentName();
  73.         }
  74.         }
  75.     }
  76.         return name;
  77.     }
  78.  
  79.     /**
  80.      * Sets the name of the component to the specified string.
  81.      * @param         name    the name of the menu component.
  82.      * @see           java.awt.MenuComponent#getName
  83.      * @since         JDK1.1
  84.      */
  85.     public void setName(String name) {
  86.     synchronized(this) {
  87.         this.name = name;
  88.         nameExplicitlySet = true;
  89.     }
  90.     }
  91.  
  92.     /**
  93.      * Returns the parent container for this menu component.
  94.      * @return    the menu component containing this menu component, 
  95.      *                 or <code>null</code> if this menu component 
  96.      *                 is the outermost component, the menu bar itself.
  97.      * @since     JDK1.0
  98.      */
  99.     public MenuContainer getParent() {
  100.     return parent;
  101.     }
  102.  
  103.     /**
  104.      * @deprecated As of JDK version 1.1,
  105.      * programs should not directly manipulate peers.
  106.      */
  107.     public MenuComponentPeer getPeer() {
  108.     return peer;
  109.     }
  110.  
  111.     /**
  112.      * Gets the font used for this menu component.
  113.      * @return   the font used in this menu component, if there is one; 
  114.      *                  <code>null</code> otherwise.
  115.      * @see     java.awt.MenuComponent#setFont
  116.      * @since   JDK1.0
  117.      */
  118.     public Font getFont() {
  119.     Font font = this.font;
  120.     if (font != null) {
  121.         return font;
  122.     }
  123.     MenuContainer parent = this.parent;
  124.     if (parent != null) {
  125.         return parent.getFont();
  126.     }
  127.     return null;
  128.     }
  129.  
  130.     /**
  131.      * Sets the font to be used for this menu component to the specified 
  132.      * font. This font is also used by all subcomponents of this menu 
  133.      * component, unless those subcomponents specify a different font. 
  134.      * @param     f   the font to be set.
  135.      * @see       java.awt.MenuComponent#getFont
  136.      * @since     JDK1.0
  137.      */
  138.     public void setFont(Font f) {
  139.     font = f;
  140.     }
  141.  
  142.     /**
  143.      * Removes the menu component's peer.  The peer allows us to modify the
  144.      * appearance of the menu component without changing the functionality of
  145.      * the menu component.
  146.      */
  147.     public void removeNotify() {
  148.         synchronized(getTreeLock()) {
  149.         MenuComponentPeer p = (MenuComponentPeer)this.peer;
  150.         if (p != null) {
  151.                 Toolkit.getEventQueue().removeSourceEvents(this);
  152.             this.peer = null;
  153.             p.dispose();
  154.         }
  155.         }
  156.     }
  157.  
  158.     /**
  159.      * Posts the specified event to the menu.
  160.      * This method is part of the Java 1.0 event system
  161.      * and it is maintained only for backwards compatibility.
  162.      * Its use is discouraged, and it may not be supported
  163.      * in the future.
  164.      * @param evt the event which is to take place
  165.      * @deprecated As of JDK version 1.1,
  166.      * replaced by <code>dispatchEvent(AWTEvent)</code>.
  167.      * @since JDK1.0
  168.      */
  169.     public boolean postEvent(Event evt) {
  170.     MenuContainer parent = this.parent;
  171.     if (parent != null) {
  172.         parent.postEvent(evt);
  173.     }
  174.     return false;
  175.     }
  176.  
  177.     /*
  178.      * Delivers an event to this component or one of its sub components.
  179.      * @param e the event
  180.      */
  181.     public final void dispatchEvent(AWTEvent e) {
  182.         dispatchEventImpl(e);
  183.     }
  184.  
  185.     void dispatchEventImpl(AWTEvent e) {
  186.         if (newEventsOnly || 
  187.             (parent != null && parent instanceof MenuComponent &&
  188.              ((MenuComponent)parent).newEventsOnly)) {
  189.             if (eventEnabled(e)) {
  190.                 processEvent(e);
  191.             } else if (e instanceof ActionEvent && parent != null) {
  192.                 ((MenuComponent)parent).dispatchEvent(new ActionEvent(parent, 
  193.                                          e.getID(),
  194.                                          ((ActionEvent)e).getActionCommand()));
  195.             }
  196.                 
  197.         } else { // backward compatibility
  198.             Event olde = e.convertToOld();
  199.             if (olde != null) {
  200.                 postEvent(olde);
  201.             }
  202.         }
  203.     }
  204.  
  205.     // REMIND: remove when filtering is done at lower level
  206.     boolean eventEnabled(AWTEvent e) {
  207.         return false;
  208.     }        
  209.     /** 
  210.      * Processes events occurring on this menu component.  
  211.      * @param e the event
  212.      * @since JDK1.1
  213.      */   
  214.     protected void processEvent(AWTEvent e) {
  215.     }
  216.  
  217.     /**
  218.      * Returns the parameter string representing the state of this  
  219.      * menu component. This string is useful for debugging. 
  220.      * @return     the parameter string of this menu component.
  221.      * @since      JDK1.0
  222.      */
  223.     protected String paramString() {
  224.     String thisName = getName();
  225.     return (thisName != null? thisName : "");
  226.     }
  227.  
  228.     /**
  229.      * Returns a representation of this menu component as a string. 
  230.      * @return  a string representation of this menu component.
  231.      * @since     JDK1.0
  232.      */
  233.     public String toString() {
  234.     return getClass().getName() + "[" + paramString() + "]";
  235.     }
  236.  
  237.     /**
  238.      * Gets this component's locking object (the object that owns the thread 
  239.      * sychronization monitor) for AWT component-tree and layout
  240.      * operations.
  241.      * @return This component's locking object.
  242.      */
  243.     protected final Object getTreeLock() {
  244.     return Component.LOCK;
  245.     }
  246.  
  247. }
  248.